﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class ProjectTreeView : System.Web.UI.UserControl
    {
        private QS_PROJECTBLL projectBLL = new QS_PROJECTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FileTree.ImageSet = TreeViewImageSet.Custom;

                BindCompany();
                BindFather();

                //TreeSet();

                // FileTree.ExpandAll();
            }
        }

        /// <summary>
        /// 绑定公司
        /// </summary>
        private void BindCompany()
        {
            Sys_User lognUser = (Sys_User)System.Web.HttpContext.Current.Session["SysUserInfo"];

            DataTable tableOrganization = new Sys_CompanyBLL().SelectByWhere("", lognUser.USER_COMPANY.ToString());
            if (tableOrganization != null && tableOrganization.Rows.Count > 0)
            {
                // setList(tableOrganization, ddlCompany);
                PublicMethod.BindDDL(ddlCompany, tableOrganization, "CMY_ID", "CMY_NAME", false);

                ddlCompany.Items.Insert(0, new ListItem("通用", "0"));

                ddlCompany.SelectedValue = "0";
            }
            else
            {
                ddlCompany.Items.Add(new ListItem("无", "0"));
            }
        }

        // static DataTable dt = null;
        /// <summary>
        ///
        /// </summary>
        public void BindFather()
        {
            FileTree.Nodes.Clear();

            string sql = @"select *
  from qs_project t where CMY_ID=" + ddlCompany.SelectedValue + @"
 order by t.n_parentid, t.n_num1, t.n_num2 ";

            DataTable dt = new BaseBLL().Query(sql);
            TreeNode node = new TreeNode();
            BindNextNodes("0", node, dt);
            var count = node.ChildNodes.Count;
            while (count != 0)
            {
                var _node = node.ChildNodes[0];
                FileTree.Nodes.Add(_node);
                count--;
            }
        }

        /// <summary>
        /// 添加子节点的方法
        /// </summary>
        /// <param name="NodeId">节点编号</param>
        /// <param name="TwoTreeNode">节点对象</param>
        public void BindNextNodes(string NodeId, TreeNode TwoTreeNode, DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["N_PARENTID"].ToString() == NodeId)
                {
                    TreeNode no = new TreeNode();
                    switch (dt.Rows[i]["N_TYPE"].ToString())
                    {
                        case "0":
                            no.ImageUrl = "~/images/levelOnesmall.jpg";
                            BindNextNodes(dt.Rows[i]["N_PROID"].ToString(), no, dt);
                            break;

                        case "1":
                            no.ImageUrl = "~/images/levelTwo.jpg";
                            BindNextNodes(dt.Rows[i]["N_PROID"].ToString(), no, dt);
                            break;

                        case "2":
                            {
                                no.ImageUrl = "~/images/levelThr_01.jpg";
                                //BindItemNodes(dt.Rows[i]["N_PROID"].ToString(), no);
                                //no.CollapseAll();
                            }
                            break;

                        default:
                            break;
                    }
                    no.Text = dt.Rows[i]["C_PRONAME"].ToString();
                    no.Value = dt.Rows[i]["N_PROID"].ToString();
                    no.NavigateUrl = "javascript:ToReport(" + dt.Rows[i]["N_PROID"].ToString() + ",0,'" + dt.Rows[i]["C_PRONAME"].ToString() + "')";
                    TwoTreeNode.ChildNodes.Add(no);
                }
            }
        }

        public void BindItemNodes(string nodeid, TreeNode _node)
        {
            var sql = "SELECT * FROM QS_PROJECTDETAIL WHERE N_PROID = " + nodeid;
            var dt = new BaseBLL().Query(sql);
            foreach (DataRow item in dt.Rows)
            {
                var node = new TreeNode();
                node.Text = item["PRO_NUM"].ToString() + "-" + item["PRO_NAME"].ToString();
                node.Value = "";
                _node.ChildNodes.Add(node);
            }
        }

        /// <summary>
        ///
        /// </summary>
        public void TreeSet()
        {
            for (int i = 0; i < FileTree.Nodes.Count; i++)
            {
                if (FileTree.Nodes[i].ChildNodes.Count > 0)
                {
                    //FileTree.Nodes[i].ImageUrl = "~/images/levelOnesmall.jpg";
                    BBQ(FileTree.Nodes[i], "~/images/levelOnesmall.jpg");
                    for (int j = 0; j < FileTree.Nodes[i].ChildNodes.Count; j++)
                    {
                        if (FileTree.Nodes[i].ChildNodes[j].ChildNodes.Count > 0)
                        {
                            BBQ(FileTree.Nodes[i].ChildNodes[j], "~/images/levelTwo.jpg");
                            //FileTree.Nodes[i].ChildNodes[j].ImageUrl = "~/images/levelTwo.jpg";

                            for (int k = 0; k < FileTree.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                            {
                                if (FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count > 0)
                                {
                                    BBQ(FileTree.Nodes[i].ChildNodes[j].ChildNodes[k], "~/images/levelThr_01.jpg");
                                    //FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ImageUrl = "~/images/levelThr_01.jpg";

                                    for (int m = 0; m < FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count; m++)
                                    {
                                        BBQ(FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m], "~/images/levelFour_01.jpg");
                                        //FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m].ImageUrl = "~/images/levelFour_01.jpg";
                                    }
                                }
                                else
                                {
                                    BBQ(FileTree.Nodes[i].ChildNodes[j].ChildNodes[k], "~/images/levelFour_01.jpg");
                                    //FileTree.Nodes[i].ChildNodes[j].ChildNodes[k].ImageUrl = "~/images/levelFour_01.jpg";
                                }
                            }
                        }
                        else
                        {
                            BBQ(FileTree.Nodes[i].ChildNodes[j], "~/images/levelFour_01.jpg");
                            //FileTree.Nodes[i].ChildNodes[j].ImageUrl = "~/images/levelFour_01.jpg";
                        }
                    }
                }
                else
                {
                    BBQ(FileTree.Nodes[i], "~/images/levelFour_01.jpg");
                    //FileTree.Nodes[i].ImageUrl = "~/images/levelFour_01.jpg";
                }
            }
        }

        public void BBQ(TreeNode node, string url)
        {
            if (node.Expanded != null && node.Expanded == false && node.Value == "")
            {
                node.ImageUrl = "";
            }
            else
            {
                node.ImageUrl = url;
            }
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindFather();

            FileTree.ExpandAll();
        }
    }
}